package com.terrace.controller.system;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.terrace.core.common.model.ResultData;
import com.terrace.db.system.mapper.entity.Dict;
import com.terrace.db.system.service.DictDataService;
import com.terrace.db.system.service.DictService;
import com.terrace.core.mybatis.entity.Pagination;
import com.terrace.core.utils.UUIDGenerator;
import com.terrace.web.annotation.Logs;
import com.terrace.web.constant.GlobalsConstant;
import com.terrace.web.model.DataGrid;

/**
 * 数据字典
 * @author jiangyg
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/dict")
public class DictController {
	
	private static final String MESSAGE_TYPE = "数据字典";
	
	@Autowired
	private DictService dictService;
	
	@Autowired
	private DictDataService dictDataService;
	
	/**
	 * 默认管理页面
	 * @param request
	 * @return
	 */
	@RequestMapping
	public ModelAndView dictList(HttpServletRequest request){
		return new ModelAndView("system/dict/dictList");
	}
	
	/**
	 * 数据表格
	 * @param request
	 * @param pagination
	 * @return
	 */
	@ResponseBody
	@RequestMapping(params = "datagrid")
	@RequiresPermissions("system:dict:view")
	public DataGrid<Dict> datagrid(Dict dict, Pagination<Dict> pagination, HttpServletRequest request) {
		DataGrid<Dict> dataGrid = new DataGrid<Dict>();
		dataGrid.setPagination(dictService.selectDictBySelective(dict, pagination));
		return dataGrid;
	}
	
	/**
	 * 新增或者修改页面
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(String id, HttpServletRequest request){
		if (StringUtils.isNotBlank(id)) {
			Dict dict = dictService.selectDictById(id);
			request.setAttribute("dict", dict);
		}
		return new ModelAndView("system/dict/dict");
	}
	
	/**
	 * 保存数据字典
	 * @param dict
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequiresRoles(GlobalsConstant.Roles.ADMIN)
	@RequestMapping(params = "saveOrUpdate")
	@RequiresPermissions("system:dict:save")
	@Logs(depict = "保存数据字典", type = GlobalsConstant.LogTypes.SYS_DICT)
	public ResultData saveOrUpdate(Dict dict, HttpServletRequest request) {
		ResultData rd = new ResultData();
		if (StringUtils.isBlank(dict.getId())) {
			if (dictService.selectDictByCode(dict.getDictCode()) != null) {
				return rd.failObjs("13104", MESSAGE_TYPE, dict.getDictCode());
			}
			dict.setId(UUIDGenerator.generate());
			dictService.saveDictSelective(dict);
			rd.successObjs("10001", MESSAGE_TYPE);
		} else {
			Dict temp = dictService.selectDictById(dict.getId());
			if (temp == null) {
				return rd.failObjs("13103", MESSAGE_TYPE);
			}
			dictService.updateByDictIdSelective(dict);
			rd.successObjs("10002", MESSAGE_TYPE);
		}
		return rd;
	}
	
	/**
	 * 删除数据字典
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(params = "delete")
	@RequiresRoles(GlobalsConstant.Roles.ADMIN)
	@RequiresPermissions("system:dict:delete")
	@Logs(depict = "删除数据字典", type = GlobalsConstant.LogTypes.SYS_DICT)
	public ResultData delete(@RequestParam(value = "id") String dictId, HttpServletRequest request) {
		ResultData rd = new ResultData();
		Dict dict = dictService.selectDictById(dictId);
		if (dict == null) {
			return rd.failObjs("13103", MESSAGE_TYPE);
		}
		if (dictDataService.selectDictDatasByCode(dict.getDictCode()).size() > 0) {
			return rd.fail("请先删除数据字典下的值");
		}
		int result = dictService.deleteByDictId(dictId);
		if (result != 1) {
			return rd.failObjs("13102", MESSAGE_TYPE);
		} else {
			rd.successObjs("10003", MESSAGE_TYPE);
		}
		return rd;
	}

}
